﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

using NUnit.Framework;


namespace INSTA.Library.Tests
{
    [TestFixture]
    public class Test_ReflectionHelper
    {
        [Test]
        public void Test_Method_IsDecoratedWith()
        {
            var p = new MockPerson();
            bool result = p.IsDecoratedWith<UniqueAttribute>();

            Assert.IsTrue(result);

        }



        [Test]
        public void Test_Method_GetUniqueAttribute_ForType()
        {
            var p = new MockPerson();
            var attribute = p.GetType().GetUniqueAttribute<UniqueAttribute>();

            Assert.IsInstanceOf<UniqueAttribute>(attribute);

        }


        [Test]
        public void Test_Method_GetUniqueAttribute_ForObject()
        {
            var p = new MockPerson();
            var attribute = p.GetUniqueAttribute<UniqueAttribute>();

            Assert.IsInstanceOf<UniqueAttribute>(attribute);

        }




        [Test]
        public static void Test_Method_SetPropertyValue()
        {
            var p = new MockPerson();

            p.SetPropertyValue("Name", "toto");

            Assert.IsTrue(p.Name == "toto");

        }

        [Test]
        public static void Test_Method_GetPropertyValue()
        {
            var p = new MockPerson() {Name = "toto"};
            var expected = p.GetPropertyValue("Name");
            

            Assert.IsTrue(p.Name == expected);

        }

        [Test]
        public static void Test_Method_GetPropertyValue_Generic()
        {
            var p = new MockPerson() { Name = "toto" };
            var expected = p.GetPropertyValue<string>("Name");

            bool result = p.Name == expected &&
                          expected is string;  

            Assert.IsTrue(result);

        }


        [Test]
        public static void Test_Method_InvokeMethod()
        {
            var p = new MockPerson();

            int x = 2;

            var result = (long)p.InvokeMethod("MultiplyByTwo", x);


            Assert.IsTrue(result == 4);

        }



        [Test]
        public static void Test_Method_InvokeMethod_Generic()
        {
            var p = new MockPerson();

            int x = 2;

            var result = p.InvokeMethod<long>("MultiplyByTwo", x);

            var expected = result == 4 && result is long;

            Assert.IsTrue(expected);

        }


        [Test]
        public void Test_Method_IsDerivedFrom_ForType()
        {
            var intType = typeof (int);
            bool result = intType.IsDerivedFrom<ValueType>();

            Assert.IsTrue(result);

        }

        [Test]
        public void Test_Method_IsDerivedFrom_ForType_ForFalse()
        {
            var formType = typeof(System.Windows.Forms.Form);
            bool result = formType.IsDerivedFrom<ValueType>();

            Assert.IsFalse(result);

        }




        [Test]
        public void Test_Method_IsDerivedFrom_ForObject()
        {
            int x = 10;
            bool result = x.IsDerivedFrom<ValueType>();

            Assert.IsTrue(result);

        }





        [Test]
        public void Test_Method_ImplementsInterface_OnType()
        {
            var result = typeof (ArrayList).ImplementsInterface<IEnumerable>();

            Assert.IsTrue(result);
        }

        [Test]
        public void Test_Method_ImplementsInterface_OnInstance()
        {
            var result = new ArrayList().ImplementsInterface<IEnumerable>();

            Assert.IsTrue(result);
        }



    }
}
